# coding=utf-8
from random import choice,sample
from collections.abc import Iterable
from Common.FontColor import outcome
from datetime import datetime, timedelta
import string
import time
import random
from faker import Factory
import numpy as np


fake = Factory().create('zh_CN')


def random_choice(data):
    '''
    随机取值
    :param data:
    :return:
    '''
    if isinstance(data, Iterable) and not isinstance(data, dict):
        result = choice(data)
        return result
    else:
        outcome('red', "该对象不可迭代,请检查确认....")


def get_data_time(value=0):
    '''
    获取当前日期前任意天数日期
    :param value:
    :return:
    '''
    today = datetime.now()
    date_time = timedelta(days=value)
    re_date = (today + date_time).strftime('%Y-%m-%d %H:%M:%S')
    return re_date


def get_data(value):
    '''
    获取当前日期前任意天数日期
    :param value:
    :return:
    '''
    today = datetime.now()
    date_time = timedelta(days=value)
    re_date = (today + date_time).strftime('%Y-%m-%d')
    return re_date


def date_add(start_date, days):
    '''

    :param start_date:
    :param days:
    :return:
    '''
    start_day = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
    delta = timedelta(days=days)
    n_days = start_day + delta

    return n_days.strftime('%Y-%m-%d %H:%M:%S')


def random_char(index=1):
    '''
    随机生成一个字符
    :param index:
    :return:
    '''
    seeds = string.digits
    random_str = random.sample(seeds, index)
    result = "".join(random_str)
    return result


def random_time():
    '''
    随机生成一个字符
    :param index:
    :return:
    '''

    return str(int(time.time()*1000))


def random_randint(start, end):
    '''
    随机生成一个字符
    :param index:
    :return:
    '''

    return random.randint(start, end)


def random_pieces():
    '''
    随机生成一个字符
    :param index:
    :return:
    '''
    pieces_list = [10, 20, 50, 100, 200]
    return choice(pieces_list)


def thead_sort():
    '''
    表头排序
    :return:
    '''
    result = choice(['', 'asc', 'desc'])
    return result


def random_str(value: str):
    '''
    字符串时间格式
    :param value:
    :return:
    '''
    return "自动化--" + value+str(int(time.time()*1000))


def random_code(value: str):
    '''
    字符串时间格式
    :param value:
    :return:
    '''
    return value+str(int(time.time()*1000))


def random_letter(index=2):
    '''
    随机生成字母组合字符串
    :param index: 生成多少个
    :return:
    '''

    lw_case = np.arange(97, 123)
    lower_list = [chr(i) for i in lw_case]
    up_case = np.arange(65, 91)
    up_list = [chr(i) for i in up_case]
    sum_list = lower_list + up_list

    return "".join(sample(sum_list, k=index))


def randint_str(value: str, start, end):
    '''
    随机数
    :param value:
    :param start:
    :param end:
    :return:
    '''
    return "自动化--" + value + str(random.randint(start, end))

def show_sort():
    '''
    展示排序
    :return:
    '''
    result = choice(['TITLE_DESC', 'default', 'TITLE_ASC', 'PUB_DATE_ASC', 'PUB_DATE_DESC'])
    return result


def get_hour_second(value):
    '''
    获取当前日期前任意天数日期
    :param value:
    :return:
    '''
    today = datetime.now()
    date_time = timedelta(days=value)
    re_date = (today + date_time).strftime('%Y-%m-%d %H:%M:%S')

    return re_date



def random_phone_number():
    """随机手机号"""
    return fake.phone_number()


def random_name():
    """随机姓名"""
    return fake.name()


def random_address():
    """随机地址"""
    return fake.address()


def random_email():
    """随机email"""
    return fake.email()


def random_ipv4():
    """随机IPV4地址"""
    return fake.ipv4()


def random_str_characters(min_chars=0, max_chars=8):
    """长度在最大值与最小值之间的随机字符串"""
    return fake.pystr(min_chars=min_chars, max_chars=max_chars)


def factory_generate_ids(starting_id=1, increment=1):
    """ 返回一个生成器函数，调用这个函数产生生成器，从starting_id开始，步长为increment。 """
    def generate_started_ids():
        val = starting_id
        local_increment = increment
        while True:
            yield val
            val += local_increment
    return generate_started_ids


def factory_choice_generator(values):
    """ 返回一个生成器函数，调用这个函数产生生成器，从给定的list中随机取一项。 """
    def choice_generator():
        my_list = list(values)
        rand = random.Random()
        while True:
            yield random.choice(my_list)
    return choice_generator


if __name__ == "__main__":
    get_data(0)